// pages/check/check.js
const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const QR = require('../../utils/weapp-qrcode.js')
const BR = require('../../utils/index.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
    codeText:'',
    codeNum:'',
    qrcodeURL:'',
    brcodeUrl:'',
    orderDetails:{},
    myInfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo')
    let userId = wx.getStorageSync('userId')
    let order_id = options.orderId
    let codeNum = `${userId} ${order_id}`
    let codeText = `order_id=${order_id}&userId=${userId}`
    this.setData({ 
      userInfo: userInfo,
      codeText: codeText,
      codeNum:codeNum
    })
    this.drawImg();

    util.request(api.orderDetails, { orderId:order_id}).then((res)=>{
      if (res.errno == 0){
        res.data.orderInfo.add_time = util.formatTime(new Date(res.data.orderInfo.add_time))
        this.setData({orderDetails:res.data})
      }else{
        util.showErrorToast(res.errmsg)
      }
    })
  },
  // setText: function (e) {
  //   this.setData({
  //     codeText: e.detail.value
  //   })
  // },
  drawImg: function () {
    // console.log(this.data.codeText);
    var imgData = QR.drawImg(this.data.codeText, {
      typeNumber: 4,
      errorCorrectLevel: 'M',
      size: 500
    })
    this.setData({
      qrcodeURL: imgData
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      let data = { 'userId': wx.getStorageSync('userId') }
      util.request(api.userInfo, data).then((res) => {
        if (res.errno == 0) {
          this.setData({ myInfo: res.data })
        }else{
          util.showErrorToast(res.errmsg)
        }
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})